//
//  Register_Vc.swift
//  LocaLink
//
//  Created by Icy on 2016/12/21.
//  Copyright © 2016年 Icy. All rights reserved.
//

/*
 owner:cy
 update:2016年12月21日16:03:29
 info: 注册界面
 model: UserModel
 viewmodel: LoginViewModel
 */
import RxSwift
import NetWorkCore
import SwiftyUserDefaults
import SwiftyJSON
import UtilCore

class Register_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    
    @IBOutlet weak var firstName_Tf: UITextField!
    @IBOutlet weak var secondName_Tf: UITextField!

    @IBOutlet weak var email_Tf: UITextField!
    
    @IBOutlet weak var mobile_Tf: UITextField!
    
    @IBOutlet weak var verificationCode_Tf: UITextField!
    
    
    @IBOutlet weak var countrycode_Tf: UITextField!
    @IBOutlet weak var password_Tf: UITextField!
    
    @IBOutlet weak var goBack_Btn: UIButton!
    @IBOutlet weak var signUp_Tf: UIButton!
    @IBOutlet weak var xy_Btn: FooterLineButton!
    /*----------------------------   自定义属性区域    ----------------------------*/
    
    var manageVm:Login_Vm?
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
         self.signUp_Tf.changeBorderColor(UIColor.signIn_Separate_Line(), cornerRadius: self.signUp_Tf.frame.size.height/2)
        
    }
    
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        self.manageVm = Login_Vm()
        self.manageVm?.registerSuccess
            .subscribe(onNext: { [unowned self] ( data ) in
                guard let info = data["info"].string, info.characters.count > 0 else{
                    return self.view.toastCompletion("register success"){ _ in
                        self.navigationController?.popViewController(animated: true)
                    }
                    
                }
                self.view.toast(info)
            })
            .disposed(by: disposeBag)
        self.manageVm?.loading.asObservable().bindTo(self.view.rx_loading).disposed(by: disposeBag)
        
        self.signUp_Tf
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
               self.toRegister()
            })
            .disposed(by: disposeBag)
        
        self.goBack_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
              self.navigationController?.popViewController(animated: true)
            })
            .disposed(by: disposeBag)
        

    }
    
    /// 点击登录
    func toRegister() -> Void {
        guard let firstName = self.firstName_Tf.text, firstName.characters.count > 0 else {
            return self.view.toast("firstName is not nil")
        }
        guard let secondName = self.secondName_Tf.text, secondName.characters.count > 0 else {
            return self.view.toast("secondName is not nil")
        }
        guard let mobile = self.mobile_Tf.text, mobile.characters.count > 0 else {
            return self.view.toast("mobile is not nil")
        }
        guard let countrycode = self.countrycode_Tf.text, countrycode.characters.count > 0 else {
            return self.view.toast("countrycode is not nil")
        }
        
        guard let email = self.email_Tf.text, email.characters.count > 0 else {
            return self.view.toast("email is not nil")
        }
        guard let password = self.password_Tf.text, password.characters.count > 0 else {
            return self.view.toast("password is not nil")
        }
        var param:[String:String] = [:]
        param["username"] = email
        param["countrycode"] = countrycode
        param["phone"] = mobile
        param["password"] = password
        param["firstname"] = firstName
        param["lastname"] = secondName
        self.manageVm?.registerSubject.onNext(param as [String : AnyObject])
        
    }

    
}
